﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Data;

namespace University.Workshop.DataAccess
{
    public static class DiagnosticDataAccess
    {
        public static bool InsertDiagnosticPatient(Diagnostic diagnostic) 
        {
            if (diagnostic != null)
            {
                string stringConnection = "Insert into Diagnostic(DateDiagnostic,NameDiagnostic,NameTreatment,AdverseCircumstances,Observation,StateDiagnostic,Patient_CiPatient,Manager_CiDoctor)values('" + diagnostic.DateDiagnostic + "','" + diagnostic.NameDiagnostics + "','" + diagnostic.NameTreatments + "','" + diagnostic.AdverseCircumstancesPatient + "','" + diagnostic.Observation + "','" + diagnostic.StateDiagnostic + "',"+diagnostic.CIPatient+","+diagnostic.CIDoctor+")";
                return ExecuteConsult(stringConnection);
            }
            else
            {
                return false;
            }
        }
        private static bool ExecuteConsult(string stringConnection)
        {
            using (DBConnection connection = new DBConnection())
            {
                if (connection.CommandExecuteConsult(stringConnection))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public static DataSet ObtainNunQuery()
        {
            string stringConnection = "SELECT MAX(IdDiagnostic) From Diagnostic";
            using (DBConnection connection = new DBConnection())
            {
                if (connection.ConsultationSelect(stringConnection))
                {
                    return connection.ExtractData(stringConnection);
                }
                else
                {
                    return null;
                }                                
            }
        }
        public static DataTable LoadDataGridViewQuery()
        {
            string stringConnection = "Select * From Diagnostic";
            using (DBConnection connection = new DBConnection())
            {
                return connection.ConsultationSelectDataTable(stringConnection);
            }

        }
        public static DataTable LoadDataGridViewSearchQuery(string searchQuery)
        {
            string stringConnection = "Select * From Diagnostic where IdDiagnostic like'%" + searchQuery + "%'";
            using (DBConnection connection = new DBConnection())
            {
                if (connection.ConsultationSelect(stringConnection))
                {
                    return connection.ConsultationSelectDataTable(stringConnection);
                }
                else
                {
                    stringConnection = "Select * From Diagnostic where DateDiagnostic like'%" + searchQuery + "%'";
                    if (connection.ConsultationSelect(stringConnection))
                    {
                        return connection.ConsultationSelectDataTable(stringConnection);
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public static DataSet LoadDataDiagnosticPatient(string searchPatient)
        {
            string stringConnection = "Select NameDiagnostic, NameTreatment, AdverseCircumstances,StateDiagnostic  From Diagnostic Where Patient_CiPatient ='" + searchPatient + "'";
            using (DBConnection connection = new DBConnection())
            {
                if (connection.ConsultationSelect(stringConnection))
                {
                    return connection.ExtractData(stringConnection);
                }
                else
                {
                    return null;
                }
            }
        }
    }
}
